﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDX.Validation
{
    /// <summary>
    /// A composite <see cref="IValidationAdapter"/>.
    /// Invokes each <see cref="IValidationAdapter"/> sequentially,
    /// returning the first non-empty message.
    /// </summary>
    class CompositeValidationAdapter : IValidationAdapter
    {
        IEnumerable<IValidationAdapter> Adapters { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeValidationAdapter"/> class.
        /// </summary>
        /// <param name="adapters">The adapters.</param>
        public CompositeValidationAdapter(IEnumerable<IValidationAdapter> adapters)
        {
            this.Adapters = adapters;
        }

        /// <summary>
        /// Gets a validation failure message.
        /// Return null or String.Empty to indicate there is no error.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public string GetMessage(Context context)
        {
            foreach (var adapter in Adapters)
            {
                var message = adapter.GetMessage(context);
                if (!string.IsNullOrEmpty(message))
                {
                    return message;
                }
            }
            return string.Empty;
        }
    }
}
